//
//  setoutShipViewController.m
//  WHB_cargo
//
//  Created by 徐国庆 on 15/12/9.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "setoutShipViewController.h"
#import "GoodsList_ViewController.h"
#import "setOutAddressViewController.h"

@interface setoutShipViewController ()<UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, UIAlertViewDelegate>

@property(nonatomic, strong)UITableView   *tableView;   // 表视图
@property(nonatomic, strong)NSArray       *array;       // 列表标题数组
@property (nonatomic,retain) UIDatePicker *datePicker;  // 日期选择器

@end

@implementation setoutShipViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSArray arrayWithObjects:@"取货日期:", @"货品类型:", @"货品重量:", @"取货方:", @"联系电话:", @"收货方:", @"联系电话:", @"补充说明:", nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitle.text = @"发货";
    self.leftButton.hidden = YES;
    [self initWithView];
    NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString*documentsDirectory =[paths objectAtIndex:0];
    
    //APP安装目录中的document目录路径
    NSString*dbPath =[documentsDirectory stringByAppendingPathComponent:@"erea.db"];
    NSLog(@"dbPath == %@", dbPath);
}

#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifer = @"setoutShipCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:indentifer];
    }
    if (indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 3 || indexPath.row == 5) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = @"请选择日期";
    } else if (indexPath.row == 1){
        cell.detailTextLabel.text = @"汽油";
    } else if (indexPath.row == 2){
        cell.detailTextLabel.text = @"20吨";
    } else if (indexPath.row == 3){
        cell.detailTextLabel.text = @"京东";
        cell.detailTextLabel.textColor = [UIColor HexStrWithColor:@"AE0000"];
    } else if (indexPath.row == 4){
        cell.detailTextLabel.text = @"13933733772";
    } else if (indexPath.row == 5){
        cell.detailTextLabel.text = @"天猫";
        cell.detailTextLabel.textColor = [UIColor HexStrWithColor:@"AE0000"];
    } else if (indexPath.row == 6){
        cell.detailTextLabel.text = @"13509890321";
    } else if (indexPath.row == 7){
        cell.detailTextLabel.text = @"轻拿轻放";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        datePicker.datePickerMode = UIDatePickerModeDate;
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文
        datePicker.locale = locale;
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [alert.view addSubview:datePicker];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
            
            //实例化一个NSDateFormatter对象
            [dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式
            NSString *dateString = [dateFormat stringFromDate:datePicker.date];
            UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
            cell.detailTextLabel.text = dateString;
            //求出当天的时间字符串
            NSLog(@"%@",dateString);
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alert addAction:ok];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:^{ }];
    } else if (indexPath.row == 1){
        UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        // 使用block传值
        // 参数1：选中的物品种类
        void(^block)(NSString *name) = ^(NSString *name){
            cell.detailTextLabel.text = name;
        };
        GoodsList_ViewController *goodsList = [[GoodsList_ViewController alloc]init];
        goodsList.sendProvince = block;
        [self.navigationController pushViewController:goodsList animated:YES];
    } else {
        
    }
}

#pragma mark “出发地”按钮点击方法
- (void)setoutAddressButtonAction:(UIButton *)button{
    NSLog(@"出发地");
    setOutAddressViewController *setoutAddressVC = [[setOutAddressViewController alloc] init];
    [self.navigationController pushViewController:setoutAddressVC animated:YES];
}

#pragma mark “目的地”按钮点击方法
- (void)aimAddressButtonAction:(UIButton *)button{
    NSLog(@"目的地");
}

#pragma mark 列表的脚"发布"点击方法
- (void)footerViewAction:(UIButton *)button{
    NSLog(@"发布");
}

/**
 *  初始化界面
 */
- (void)initWithView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - 20) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableHeaderView = [self tableViewHeaderView];
    self.tableView.tableFooterView = [self tableFooterView];
    [self.view addSubview:self.tableView];
}

/**
 *  列表头
 *
 *  @return 列表头
 */
- (UIView *)tableViewHeaderView{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 80)];
    
    // 上边的线
    UILabel *topLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 0.5)];
    topLineLabel.backgroundColor = [UIColor HexStrWithColor:@"F0F0F0"];
    [bottomView addSubview:topLineLabel];
    
    // "出发地"
    UILabel *setoutAddressButton = [[UILabel alloc] init];
    setoutAddressButton.frame = CGRectMake(0, 0, kUIScreenWidth / 2, 40);
    setoutAddressButton.text = @"北京";
    setoutAddressButton.textAlignment = NSTextAlignmentCenter;
    [bottomView addSubview:setoutAddressButton];
    
    // "出发地"与“目的地”之间的箭头
    UIImageView *jxImaegView = [[UIImageView alloc] initWithFrame:CGRectMake(kUIScreenWidth / 2 - 12 / 2, 40 / 2 - 14 / 2, 12, 14)];
    jxImaegView.image = [UIImage imageNamed:@"jt"];
    [bottomView addSubview:jxImaegView];
    
    // “目的地”
    UILabel *aimAddressButton = [[UILabel alloc] init];
    aimAddressButton.frame = CGRectMake(kUIScreenWidth / 2, 0, kUIScreenWidth / 2, 40);
    aimAddressButton.text = @"杭州";
    aimAddressButton.textAlignment = NSTextAlignmentCenter;
    [bottomView addSubview:aimAddressButton];
    
    // “出发地”“目的地”下的线
    UILabel *setoutAddressBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, setoutAddressButton.frame.origin.y + setoutAddressButton.frame.size.height, kUIScreenWidth, 0.5)];
    setoutAddressBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"F0F0F0"];
    [bottomView addSubview:setoutAddressBottomLineLabel];
    
    // "出发地"下的按钮
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    firstButton.frame = CGRectMake(0, setoutAddressBottomLineLabel.frame.origin.y, kUIScreenWidth / 2, 40);
    [firstButton addTarget:self action:@selector(setoutAddressButtonAction:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:firstButton];
    
    // "出发地"按钮下的图
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kUIScreenWidth / 4 - 5, 30, 10, 7)];
    firstImageView.image = [UIImage imageNamed:@"xia"];
    [firstButton addSubview:firstImageView];
    
    // “目的地”下的按钮
    UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
    secondButton.frame = CGRectMake(kUIScreenWidth / 2, setoutAddressBottomLineLabel.frame.origin.y, kUIScreenWidth / 2, 40);
    [secondButton addTarget:self action:@selector(aimAddressButtonAction:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:secondButton];
    
    // “目的地”按钮下的图
    UIImageView *secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kUIScreenWidth / 4 - 5 , 30, 10, 7)];
    secondImageView.image = [UIImage imageNamed:@"xia"];
    [secondButton addSubview:secondImageView];
    
    // “出发地”按钮和“目的地”按钮下的线
    UILabel *firstAndSecondBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, firstButton.frame.origin.y + firstButton.frame.size.height, kUIScreenWidth, 0.5)];
    firstAndSecondBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"F0F0F0"];
    [bottomView addSubview:firstAndSecondBottomLineLabel];
    
    // “出发地”和“目的地”之间的竖线
    UILabel *setoutAndAimBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(kUIScreenWidth / 2, 45, 1, firstAndSecondBottomLineLabel.frame.origin.y - 45 - 5)];
    setoutAndAimBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];
    [bottomView addSubview:setoutAndAimBottomLineLabel];
    
    return bottomView;
}

/**
 *  列表的脚
 *
 *  @return 列表的脚
 */
- (UIView *)tableFooterView{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 100)];
    // “发布”
    UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    publishButton.frame = CGRectMake(20, 20, kUIScreenWidth - 40, 40);
    publishButton.layer.masksToBounds = YES;
    publishButton.layer.cornerRadius = 5;
    [publishButton setTitle:@"发布" forState:UIControlStateNormal];
    [publishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    publishButton.backgroundColor = [UIColor HexStrWithColor:@"CE0000"];
    publishButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [bottomView addSubview:publishButton];
    [publishButton addTarget:self action:@selector(footerViewAction:) forControlEvents:UIControlEventTouchDown];
    
    return bottomView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
